//
//  BDAlertViewSubTitleCell.m
//  BDAlertViewDemo
//
//  Created by Jaesun on 17/1/4.
//  Copyright © 2017年 S.J. All rights reserved.
//

#import "BDAlertViewSubTitleCell.h"

@interface BDAlertViewSubTitleCell()


@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UILabel *subTitleLabel;


@end

@implementation BDAlertViewSubTitleCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    
    UILabel *topLine = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 250, 0.5)];
    topLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    [self.contentView addSubview:topLine];
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0.5, 180, 43.5)];
    self.titleLabel.font = [UIFont systemFontOfSize:15.f];
    self.titleLabel.numberOfLines = 0;
    // self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.titleLabel];
    
    self.subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.titleLabel.frame), 0.5, 70, 43.5)];
    self.subTitleLabel.font = [UIFont systemFontOfSize:13.f];
    self.subTitleLabel.numberOfLines = 0;
    self.subTitleLabel.textColor = [UIColor lightGrayColor];
    self.subTitleLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:self.subTitleLabel];
}

- (void)setTitle:(NSString *)title {
    _title = title;
}

- (void)setSubTitle:(NSString *)subTitle {
    _subTitle = subTitle;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.titleLabel.text = self.title;
    
    CGFloat titleHeight = [self heightForString:self.title];
    
    if (43.5 < titleHeight) {
        
        CGRect frame = self.titleLabel.frame;
        frame.size.height = titleHeight;
        self.titleLabel.frame = frame;
    }
    
    self.subTitleLabel.text = self.subTitle;
}

- (CGFloat)heightForString:(NSString *)str {
    
    CGRect rect = [str boundingRectWithSize:CGSizeMake(180, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.f]} context:nil];
    return rect.size.height;
}


@end
